package cn.com.sbell.vss.csg.vap;

import org.dom4j.Element;
import org.dom4j.DocumentHelper;
import org.dom4j.Document;
import cn.com.sbell.vss.csg.vap.bean.AlarmConfiguration;
import cn.com.sbell.vss.csg.vsclient.VsClient;
import cn.com.sbell.vss.csg.vsclient.util.PropertiesHolder;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.VssConstants;


public class GetVsAlarmConfiguration extends ProtectedVsCommander {

	@Override
	protected Element executeCommand(VsClient vsClient, PropertiesHolder holder, Element root) {
		// TODO Auto-generated method stub
		
		AlarmConfiguration alarmConfiguration=null;
		try {
			alarmConfiguration=vsClient.getVsAlarmConfiguration();
		} catch (Exception e) {
			// TODO: handle exception
			throw new VssException(VssConstants.CODE_GET_ALARM_FAILED);
		}
		if(null==alarmConfiguration)
		{
			throw new VssException(VssConstants.CODE_GET_ALARM_FAILED);
		}
		Element configuration=root;
		configuration.addElement("connectionDetectionEnabled").setText(Boolean.toString(alarmConfiguration.connectionDetectionEnabled));
		configuration.addElement("diskFullAlarmEnabled").setText(Boolean.toString(alarmConfiguration.diskFullAlarmEnabled));
		configuration.addElement("diskFullHighThreshold").setText(Integer.toString(nullReturnZero(alarmConfiguration.diskFullHighThreshold)));
		configuration.addElement("diskFullLowThreshold").setText(Integer.toString(nullReturnZero(alarmConfiguration.diskFullLowThreshold)));
		configuration.addElement("diskErrorAlarmEnabled").setText(Boolean.toString(alarmConfiguration.diskErrorAlarmEnabled));
		configuration.addElement("alarmPreRecordTime").setText(Integer.toString(alarmConfiguration.alarmPreRecordTime));
		configuration.addElement("alarmRecordTime").setText(Integer.toString(alarmConfiguration.alarmRecordTime));
//		int alarmOutputTime=alarmConfiguration.alarmOutputTime;
//		alarmOutputTime=alarmOutputTime==-1?0:alarmOutputTime;
		configuration.addElement("alarmOutputTime").setText(Integer.toString(alarmConfiguration.alarmOutputTime));
		configuration.addElement("photoResolution").setText(Integer.toString(alarmConfiguration.photoResolution));
		configuration.addElement("photoQuality").setText(Integer.toString(alarmConfiguration.photoQuality));
		configuration.addElement("shootTimes").setText(Integer.toString(alarmConfiguration.shootTimes));
		configuration.addElement("imageFormat").setText(alarmConfiguration.imageFormat);
		for (int i = 0; i < alarmConfiguration.gpinAlarm.length; i++) {
			Element gpinAlarm=configuration.addElement("gpinAlarm");
			gpinAlarm.addElement("channelNumber").setText(Integer.toString(alarmConfiguration.gpinAlarm[i]
					           .channelNumber));
			gpinAlarm.addElement("gpinAlarmEnabled").setText(Boolean.toString(alarmConfiguration.gpinAlarm[i].gpinAlarmEnable));
			gpinAlarm.addElement("gpinAlarmState").setText(Integer.toString(alarmConfiguration.gpinAlarm[i].gpinAlarmStatus));
			
			for (int j = 0; j < alarmConfiguration.gpinAlarm[i].gpinAlarmOutput.length; j++) {
			Element gpinAlarmOutput=gpinAlarm.addElement("gpinAlarmOutput");
			gpinAlarmOutput.addElement("outputChannelNumber").setText(Integer.toString(alarmConfiguration.gpinAlarm[i]
			                                                                                                         .gpinAlarmOutput[j].channelNumber));
			gpinAlarmOutput.addElement("alarmState").setText(Integer.toString(alarmConfiguration.gpinAlarm[i].gpinAlarmOutput[j].alarmState));
			}
			for (int j = 0; j < alarmConfiguration.gpinAlarm[i].gpinAlarmRecord.length; j++) {
			gpinAlarm.addElement("gpinAlarmRecord").setText(Integer.toString(alarmConfiguration.gpinAlarm[i]
			                                                                                              .gpinAlarmRecord[j]));
			}
			for (int j = 0; j < alarmConfiguration.gpinAlarm[i].gpinAlarmShoot.length; j++) {
				gpinAlarm.addElement("gpinAlarmShoot").setText(Integer.toString(alarmConfiguration.gpinAlarm[i].gpinAlarmShoot[j]));
			}			
		}
		for (int i = 0; i < alarmConfiguration.gpoutAlarm.length; i++) {
			Element gpoutAlarm=configuration.addElement("gpoutAlarm");
			gpoutAlarm.addElement("channelNumber").setText(Integer.toString(alarmConfiguration.gpoutAlarm[i]
			                                                                                              .channelNumber));
			gpoutAlarm.addElement("gpoutDeviceName").setText(alarmConfiguration.gpoutAlarm[i].gpoutDeviceName);
			gpoutAlarm.addElement("gpoutType").setText(Integer.toString(alarmConfiguration.gpoutAlarm[i].gpoutType));
			gpoutAlarm.addElement("gpoutActivatedTime").setText(
					intToTwoDigitString(alarmConfiguration.gpoutAlarm[i].gpoutActivatedTime.getStartHour())
					+intToTwoDigitString(alarmConfiguration.gpoutAlarm[i].gpoutActivatedTime.getStartMin())
					+"-"
					+intToTwoDigitString(alarmConfiguration.gpoutAlarm[i].gpoutActivatedTime.getStopHour())
					+intToTwoDigitString(alarmConfiguration.gpoutAlarm[i].gpoutActivatedTime.getStopMin()));
		}
		for (int i = 0; i < alarmConfiguration.videoAlarm.length; i++) {
			Element cameraAlarm=configuration.addElement("cameraAlarm");
			cameraAlarm.addElement("cameraNumber").setText(Integer.toString(alarmConfiguration.videoAlarm[i]
			                                                                                         .videoId));
			cameraAlarm.addElement("videoLoseAlarmEnabled").setText(Boolean.toString(alarmConfiguration.videoAlarm[i].videoLoseAlarmEnabled));
			for (int j = 0; j < alarmConfiguration.videoAlarm[i].videoLoseAlarmOutput.length; j++) {
				Element videoLoseAlarmOutput=cameraAlarm.addElement("videoLoseAlarmOutput");
				videoLoseAlarmOutput.addElement("outputChannelNumber").setText(Integer.toString(alarmConfiguration.videoAlarm[i].
						videoLoseAlarmOutput[j].channelNumber));
				videoLoseAlarmOutput.addElement("alarmState").setText(Integer.toString(alarmConfiguration.videoAlarm[i].videoLoseAlarmOutput[j].alarmState));
			}
			cameraAlarm.addElement("hideDetectionAlarmEnabled").setText(Boolean.toString(alarmConfiguration.videoAlarm[i].hideDetectionAlarmEnabled));
			cameraAlarm.addElement("hideDetectionAlarmTime").setText(
					intToTwoDigitString(alarmConfiguration.videoAlarm[i].hideDetectionAlarmTime.getStartHour())
					+intToTwoDigitString(alarmConfiguration.videoAlarm[i].hideDetectionAlarmTime.getStartMin())
					+"-"
					+intToTwoDigitString(alarmConfiguration.videoAlarm[i].hideDetectionAlarmTime.getStopHour())
					+intToTwoDigitString(alarmConfiguration.videoAlarm[i].hideDetectionAlarmTime.getStopMin()));
			cameraAlarm.addElement("hideDetectionX").setText(Integer.toString(alarmConfiguration.videoAlarm[i].hideDetectionX));
			cameraAlarm.addElement("hideDetectionY").setText(Integer.toString(alarmConfiguration.videoAlarm[i].hideDetectionY));
			cameraAlarm.addElement("hideDetectionWidth").setText(Integer.toString(alarmConfiguration.videoAlarm[i].hideDetectionWidth));
			cameraAlarm.addElement("hideDetectionHeight").setText(Integer.toString(alarmConfiguration.videoAlarm[i].hideDetectionHeight));
			for (int j = 0; j < alarmConfiguration.videoAlarm[i].hideDetectionAlarmOutput.length; j++) {
			Element hideDetectionAlarmOutput=cameraAlarm.addElement("hideDetectionAlarmOutput");
			hideDetectionAlarmOutput.addElement("outputChannelNumber").setText(Integer.toString(alarmConfiguration.videoAlarm[i].hideDetectionAlarmOutput[j].channelNumber));
			hideDetectionAlarmOutput.addElement("alarmState").setText(Integer.toString(alarmConfiguration.videoAlarm[i].hideDetectionAlarmOutput[j].alarmState));
			}
			cameraAlarm.addElement("motionDetectionAlarmEnabled").setText(Boolean.toString(alarmConfiguration.videoAlarm[i].motionDetectionAlarmEnabled));
			cameraAlarm.addElement("motionDetectionAlarmTime").setText(
					intToTwoDigitString(alarmConfiguration.videoAlarm[i].motionDetectionAlarmTime.getStartHour())
					+intToTwoDigitString(alarmConfiguration.videoAlarm[i].motionDetectionAlarmTime.getStartMin())
					+"-"
					+intToTwoDigitString(alarmConfiguration.videoAlarm[i].motionDetectionAlarmTime.getStopHour())
					+intToTwoDigitString(alarmConfiguration.videoAlarm[i].motionDetectionAlarmTime.getStopMin()));
			StringBuffer sb=new StringBuffer();
			byte[] blocks=alarmConfiguration.videoAlarm[i].motionDetectionBlocks;
			for (int j = 0; j < blocks.length; j++) {
				sb.append(Byte.toString(blocks[j]));
			}
			cameraAlarm.addElement("motionDetectionBlocks").setText(sb.toString());
			cameraAlarm.addElement("motionDetectionFrequency").setText(Integer.toString(alarmConfiguration.videoAlarm[i].motionDetectionFrequency));
			cameraAlarm.addElement("motionDetectionSensitivity").setText(Integer.toString(alarmConfiguration.videoAlarm[i].motionDetectionSensitivity));
			for (int j = 0; j < alarmConfiguration.videoAlarm[i].motionDetectionAlarmOutput.length; j++) {
				Element motionDetectionAlarmOutput=cameraAlarm.addElement("motionDetectionAlarmOutput");
				motionDetectionAlarmOutput.addElement("outputChannelNumber").setText(Integer.toString(alarmConfiguration.videoAlarm[i]
				                                                                                                                    .motionDetectionAlarmOutput[j].channelNumber));
				motionDetectionAlarmOutput.addElement("alarmState").setText(Integer.toString(alarmConfiguration.videoAlarm[i].motionDetectionAlarmOutput[j].alarmState));
			}
			for (int j = 0; j < alarmConfiguration.videoAlarm[i].motionDetectionAlarmRecord.length; j++) {
				cameraAlarm.addElement("motionDetectionAlarmRecord").setText(Integer.toString(alarmConfiguration.videoAlarm[i].motionDetectionAlarmRecord[j]));
			}
			
			for (int j = 0; j < alarmConfiguration.videoAlarm[i].motionDetectionAlarmShoot.length; j++) {
				cameraAlarm.addElement("motionDetectionAlarmShoot").setText(Integer.toString(alarmConfiguration.videoAlarm[i].motionDetectionAlarmShoot[j]));
		}
		}			
		return root;
	}
	private Integer nullReturnZero(Integer i)
	{
		return (null==i)?0:i;
	}
	private static String intToTwoDigitString(int a)
	{
		return paddToTwoDigit(a)+Integer.toString(a);
	}
	private static String paddToTwoDigit(int a)
	{
		if(a<10)
		{
			return "0";
		}else
		{
			return"";
		}
		
	}
	public static void main(String[] args) {
		
		System.out.println(intToTwoDigitString(9));
	}
}
